﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using AEM.Properties;
using AEM.Utils;

namespace AEM.Basic
{
    /// <summary>
    /// SMSSetting.xaml 的交互逻辑
    /// </summary>
    public partial class SMSSetting : Window
    {
        public SMSSetting()
        {
            InitializeComponent();
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(TitleBar_MouseLeftButtonDown);
            this.Loaded += new RoutedEventHandler(SMSSetting_Loaded);
            this.btnSave.Click += new RoutedEventHandler(btnSave_Click);
            this.num1.TextChanged += new TextChangedEventHandler(num_TextChanged);
            this.num2.TextChanged += new TextChangedEventHandler(num_TextChanged);
            this.num3.TextChanged += new TextChangedEventHandler(num_TextChanged);
            this.num4.TextChanged += new TextChangedEventHandler(num_TextChanged);
            this.num5.TextChanged += new TextChangedEventHandler(num_TextChanged);
        }

        void num_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (!string.IsNullOrEmpty(tb.Text.Trim()) && !tb.Text.Trim().isNumeric())
            {
                tb.Text = tb.Text.Substring(0, tb.Text.Length - 1);
                this.msg.Text = string.Format(this.TryFindResource("MustBeNumber") as string, this.TryFindResource("PhoneForSMS") as string);
            }
        }

        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string sms = this.num1.Text.Trim() + ",";
            sms += this.num2.Text.Trim() + ",";
            sms += this.num3.Text.Trim() + ",";
            sms += this.num4.Text.Trim() + ",";
            sms += this.num5.Text.Trim() + ",";
            Settings.Default.SmsPhones = sms;
            Settings.Default.Save();
            this.msg.Text = this.TryFindResource("successfuladd") as string;
        }

        void SMSSetting_Loaded(object sender, RoutedEventArgs e)
        {
            string smsnums = Settings.Default.SmsPhones;
            if (!string.IsNullOrEmpty(smsnums))
            {
                string[] arr = smsnums.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < arr.Length; i++)
                {
                    TextBox t = this.FindName("num" + (i + 1).ToString()) as TextBox;
                    if (null != t)
                        t.Text = arr[i];
                }
            }
        }

        void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
